const jwt = require('jsonwebtoken')

const validateToken = function (req, res, next) {
  const authorizationHeaader = req.headers.authorization;
  let result;
  if (authorizationHeaader) {
    const token = req.headers.authorization.split(' ')[1]; // Bearer <token>
    const options = {
      expiresIn: '2d',
      issuer: 'https://51reader.com'
    };
    try {
      result = jwt.verify(token, process.env.JWT_SECRET, options);
      req.decoded = result;
      next();
    } catch (err) {
      throw new Error(err)
    }
  } else {
    result = {
      error: `Authentication error. Token required.`,
      status: 401
    }
    res.status(401).send(result)
  }
}

module.exports = {
  validateToken
}
